import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/index.dart';
import 'package:yp/config/rules.dart';
import 'package:yp/services/api.dart';
import 'package:yp/widgets/button_widget.dart';
import 'package:yp/widgets/index.dart';
import 'package:yp/widgets/task_item.dart';

import '../../router.dart';

/// 会员任务
class VipTask extends StatefulWidget {
  const VipTask({Key key}) : super(key: key);

  @override
  _VipTaskState createState() => _VipTaskState();
}

class _VipTaskState extends State<VipTask> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  var userInformationData;

  /*
 award_type 601, "生日有礼"
award_type 603, "完善信息有礼"
award_type 604, "评价有礼"
award_type 605, "首单有礼"
award_type 606, "消费有礼"
award_type 607, "分享链接有礼"
award_type 608, "储值有礼"
award_type 610, "注册有礼"
    */
  List taskList = [];
  Map taskDay = {'full_message': '', 'complete_award_sign': 0, 'complete_activity_sign': 0, 'sign_in_award_type': 0, 'sign_in_rule_type': 1, 'invite_activity_sign': 1, 'do_tasks': 1};

  getTask() {
    // 获取用户信息
    var params3 = {
      'access_token': SpUtil.getString('token'),
      'mobile': SpUtil.getString('number'),
    };
    Api.searchMember(params3).listen((event) {
      if (event['code'] == 0) {
        userInformationData = event;
      } else {
        userInformationData = null;
      }
    });
    var params = {
      "channel_num_id": 98,
      "use_platform": 3,
      "sub_unit_num_id": 102002,
      // "ec_shop_num_id": SpUtil.getInt('storeShopNum'),
      "ec_shop_num_id": SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
      'access_token': SpUtil.getString('token'),
      "data_sign": 0,
    };
    Api.surpriseQuery(params).listen((event) {
      if (event['code'] == 0) {
        setState(() {
          if (event['member_surprise_task_infos'] != null && event['member_surprise_task_infos'].length > 0) {
            taskList = event['member_surprise_task_infos'];
            print(taskList);
            // a.map((e) => null)
          }
        });
      }
    });
    Api.surpriseDayQuery(params).listen((event) {
      if (event['code'] == 0) {
        setState(() {
          taskDay = event;
        });
      }
    });
  }

  Widget getWigets(param) {
    Widget wid = Container();
    print('底部判断');

    if (rules('a8', param)) {
      wid = TaskItem('assets/images/task_stored.png', '储值有礼', '去完成', () {
        Get.toNamed(RouteConfig.myWallet).then((value) {});
      }, subName: '储值获得更多积分等其他奖励');
    } else if (rules('a9', param)) {
      wid = TaskItem('assets/images/task_consum.png', '消费有礼', '去消费', () {
        Get.toNamed(RouteConfig.consumeGift);
      }, subName: '消费获得更多积分等其他奖励');
    } else if (rules('a10', param)) {
      wid = TaskItem('assets/images/task_common.png', '评论有礼', '去评论', () {
        Get.toNamed(RouteConfig.order, arguments: {'index': 4});
      }, subName: '评论获得更多积分等其他奖励');
    }
    print(wid);
    return wid;
  }

  Widget getDayWigets(param) {
    Widget wid = Container();
    if (rules('a11', param)) {
      wid = TaskItem('assets/images/task_signin.png', '完善信息有礼', '去完善', () {
        Get.toNamed(RouteConfig.memberInformation, arguments: userInformationData);
      });
    } else if (rules('a12', param)) {
      wid = TaskItem('assets/images/task_signin.png', '每日签到，送积分', '去签到', () {
        Get.toNamed(RouteConfig.signIn);
      });
    }
    // }else if(rules('a13',param)){
    //     wid =   TaskItem('assets/images/task_invite.png', '邀请好友', '去邀请', () {});
    // }

    return wid;
  }

  // List<Map<String, dynamic>> taskList = [
  //   {
  //     'normal': [
  //       {
  //         'id': '1',
  //         'icon': '',
  //         'name': '',
  //         'nameTips': '',
  //         'subName': '',
  //         'btnName': '',
  //         'func': () {}
  //       },
  //       {}
  //     ]
  //   },
  //   {'Surprise': ''}
  // ];

  @override
  void initState() {
    getTask();
    super.initState();
    _controller = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: YPNavigationBar(
        '会员任务',
        backgroundColor: Color(0xff2a2c37),
        titleColor: EsColors.White,
        backBtnColor: EsColors.White,
      ),
      body: CustomScrollView(slivers: [
        SliverToBoxAdapter(
          child: Container(
            width: double.infinity,
            height: 100,
            child: Center(
              child: GestureDetector(
                onTap: () {
                  Get.toNamed(RouteConfig.myReward, arguments: userInformationData);
                  //print('查看我的奖励');
                },
                child: Container(
                  padding: EdgeInsets.symmetric(vertical: 4, horizontal: 12),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text('查看我的奖励', style: TextStyle(fontSize: EsSizes.XS, color: Color(0xff855533), fontWeight: EsFontWeights.Normal)),
                      SizedBox(width: 4),
                      Icon(
                        Icons.arrow_forward_ios,
                        size: EsSizes.XXS,
                        color: Color(0xff855533),
                      )
                    ],
                  ),
                  decoration: BoxDecoration(
                      gradient: LinearGradient(colors: [
                        Color(0xffffebb3),
                        Color(0xffe9bc6a),
                      ]),
                      borderRadius: BorderRadius.all(Radius.circular(12))),
                ),
              ),
            ),
            decoration: BoxDecoration(image: DecorationImage(image: AssetImage('assets/images/wallet_image.png'), fit: BoxFit.fill)),
          ),
        ),
        SliverToBoxAdapter(
            child: Padding(
          padding: const EdgeInsets.only(left: 12),
          child: Column(crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start, children: [
            SizedBox(height: 12),
            _title('日常任务'),
            SizedBox(height: 10),
            Visibility(
                visible: rules('a11', taskDay),
                child: TaskItem('assets/images/my_task_info.png', '完善信息有礼', '去完善', () {
                  Get.toNamed(RouteConfig.memberInformation, arguments: userInformationData);
                })),
            YpLine(),
            Visibility(
                visible: rules('a12', taskDay),
                child: TaskItem('assets/images/task_signin.png', '每日签到，送积分', '去签到', () {
                  Get.toNamed(RouteConfig.signIn);
                })),
            YpLine(),
            Visibility(visible: rules('a19', taskDay), child: TaskItem('assets/images/task_invite.png', '邀请好友', '去邀请', () {
               Get.toNamed(RouteConfig.inviteFriend);
            })),
          ]),
        )),
        SliverToBoxAdapter(
          child: YpLine(height: 20, thickness: 20),
        ),
        SliverToBoxAdapter(
            child: Padding(
          padding: const EdgeInsets.only(left: 12),
          child: Column(crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start, children: [
            SizedBox(height: 12),
            _title('惊喜任务'),
            SizedBox(height: 10),
            Column(
              children: taskList.length > 0
                  ? taskList.map<Widget>((e) {
                      return getWigets(e);
                    }).toList()
                  : [],
            )
          ]),

          // TaskItem('assets/images/task_consum.png', '消费有礼', '取消费', () {}, subName: '消费获得更多积分等其他奖励'),
          // YpLine(),
          // TaskItem('assets/images/task_stored.png', '评论有礼', '去评论', () {}, subName: '评论获得更多积分等其他奖励'),
          // YpLine(),
          // TaskItem('assets/images/task_invite.png', '储值有礼', '去完成', () {}, subName: '储值获得更多积分等其他奖励'),
          // YpLine(),
          // TaskItem('assets/images/task_share.png', '分享有礼', '去完成', () {}, subName: '分享获得更多积分等其他奖励'),
          // YpLine(),
          // TaskItem('assets/images/task_first_order.png', '首单有礼', '去完成', () {
          //   print('点了点了');
          // },
          //     subName: '邀请获得更多积分等其他奖励',
          //     tips: GestureDetector(
          //       onTap: () {},
          //       child: Padding(
          //         padding: const EdgeInsets.all(4.0),
          //         child: Icon(Icons.help_outline, size: 14, color: EsColors.ContentSecondary),
          //       ),
          //     ),
          //     finish: true),
        ))
      ]),
    );
  }

  Widget _title(String name) {
    return Text(name ?? '', style: TextStyle(fontSize: EsSizes.LG, color: EsColors.TitleSecondary, fontWeight: EsFontWeights.Medium));
  }
}
